package chaxun;

public class AddressBook {
    public static void main(String[] args) {
        // 创建 Student 数组并初始化
        Student[] students = new Student[3];
        students[0] = new Student("张三", 20, "男", "13800138000", "北京市", "zhangsan@example.com");
        students[1] = new Student("李四", 22, "女", "13900139000", "上海市", "lisi@example.com");
        students[2] = new Student("王五", 21, "男", "13700137000", "广州市", "wangwu@example.com");

        // 按 name 查询
        String searchName="张三";
        Student foundByName=searchByname(students,searchName);
        if(foundByName!=null){
            System.out.println("按姓名");
            System.out.println("姓名"+foundByName.getName());
            System.out.println("年龄"+foundByName.getAge());
            System.out.println("性别"+foundByName.getGender());
            System.out.println("手机"+foundByName.getPhone());
            System.out.println("地址"+foundByName.getAddress());
            System.out.println("邮箱"+foundByName.getEmail());
        }
        // 按 email 查询
        String searchEmail="lisi@example.com";
       Student foundByEmail=searchByEmail(students,searchEmail);
        if (foundByEmail!=null){
            System.out.println("按邮箱:");
            System.out.println("姓名"+foundByEmail.getName());
            System.out.println("年龄"+foundByEmail.getAge());
            System.out.println("性别"+foundByEmail.getGender());
            System.out.println("电话"+foundByEmail.getPhone());
            System.out.println("地址"+foundByEmail.getAddress());
            System.out.println("邮箱"+foundByEmail.getEmail());
        }
        else {
            System.out.println("未找到学号"+searchEmail+"的学生");
        }
        // 按 address 查询
        String searchAddress = "深圳市";
        Student foundByAddress = searchByAddress(students, searchAddress);
        if (foundByAddress != null) {
            System.out.println("按地址找到的学生信息：");
            System.out.println("姓名：" + foundByAddress.getName());
            System.out.println("年龄：" + foundByAddress.getAge());
            System.out.println("性别：" + foundByAddress.getGender());
            System.out.println("电话：" + foundByAddress.getPhone());
            System.out.println("地址：" + foundByAddress.getAddress());
            System.out.println("邮箱：" + foundByAddress.getEmail());
        } else {
            System.out.println("未找到地址为 " + searchAddress + " 的学生");
        }
    }

    // 按姓名查询学生
    public static Student searchByname(Student[] students, String name){
        for(Student student:students){
            if (student.getName().equals(name));
            {
                return student;
            }
        }
        return null;
    }

    // 按邮箱查询学生
    public static Student searchByEmail(Student[] students, String email){
        for(Student student:students){//遍历所有学生
            if(student.getEmail().equals(email)){
                return student;
            }
        }
        return null;//必选在外面不然一个直接跳出循环了
    }

    // 按地址查询学生
    public static Student searchByAddress(Student[] students, String address) {
        for (Student student : students) {
            if (student.getAddress().equals(address)) {
                return student;
            }
        }
        return null;
    }
}
